from django.test import TestCase
from django.contrib.auth.models import User
from django.utils import timezone
from .models import Notification
from .views import get_user_notifications

class NotificationModelTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='testuser', password='testpass')
        self.admin_user = User.objects.create_user(username='admin', password='adminpass')
        
    def test_notification_creation(self):
        """测试通知创建"""
        notification = Notification.objects.create(
            title='测试通知',
            content='这是一个测试通知内容',
            target_roles=['customer'],
            priority='medium',
            start_date=timezone.now(),
            end_date=timezone.now() + timezone.timedelta(days=1),
            created_by=self.admin_user,
            is_active=True
        )
        
        self.assertEqual(notification.title, '测试通知')
        self.assertEqual(notification.priority, 'medium')
        self.assertTrue(notification.is_active)
        
    def test_notification_str(self):
        """测试通知的字符串表示"""
        notification = Notification.objects.create(
            title='测试通知',
            content='这是一个测试通知内容',
            target_roles=['customer'],
            priority='medium',
            start_date=timezone.now(),
            end_date=timezone.now() + timezone.timedelta(days=1),
            created_by=self.admin_user,
            is_active=True
        )
        
        self.assertEqual(str(notification), '测试通知')
        
    def test_priority_class(self):
        """测试优先级CSS类"""
        notification = Notification.objects.create(
            title='测试通知',
            content='这是一个测试通知内容',
            target_roles=['customer'],
            priority='high',
            start_date=timezone.now(),
            end_date=timezone.now() + timezone.timedelta(days=1),
            created_by=self.admin_user,
            is_active=True
        )
        
        self.assertEqual(notification.get_priority_class, 'danger')
        
        notification.priority = 'medium'
        self.assertEqual(notification.get_priority_class, 'warning')
        
        notification.priority = 'low'
        self.assertEqual(notification.get_priority_class, 'info')

class NotificationViewTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(username='testuser', password='testpass')
        self.admin_user = User.objects.create_user(username='admin', password='adminpass')
        
    def test_get_user_notifications(self):
        """测试获取用户通知"""
        # 创建有效通知
        notification = Notification.objects.create(
            title='测试通知',
            content='这是一个测试通知内容',
            target_roles=['customer'],
            priority='medium',
            start_date=timezone.now() - timezone.timedelta(hours=1),
            end_date=timezone.now() + timezone.timedelta(days=1),
            created_by=self.admin_user,
            is_active=True
        )
        
        # 获取用户通知
        notifications = get_user_notifications(self.user)
        self.assertEqual(len(notifications), 1)
        self.assertEqual(notifications[0].title, '测试通知')
        
    def test_get_user_notifications_expired(self):
        """测试过期通知不显示"""
        # 创建过期通知
        notification = Notification.objects.create(
            title='过期通知',
            content='这是一个过期通知内容',
            target_roles=['customer'],
            priority='medium',
            start_date=timezone.now() - timezone.timedelta(days=2),
            end_date=timezone.now() - timezone.timedelta(days=1),
            created_by=self.admin_user,
            is_active=True
        )
        
        # 获取用户通知
        notifications = get_user_notifications(self.user)
        self.assertEqual(len(notifications), 0)
        
    def test_get_user_notifications_inactive(self):
        """测试停用通知不显示"""
        # 创建停用通知
        notification = Notification.objects.create(
            title='停用通知',
            content='这是一个停用通知内容',
            target_roles=['customer'],
            priority='medium',
            start_date=timezone.now() - timezone.timedelta(hours=1),
            end_date=timezone.now() + timezone.timedelta(days=1),
            created_by=self.admin_user,
            is_active=False
        )
        
        # 获取用户通知
        notifications = get_user_notifications(self.user)
        self.assertEqual(len(notifications), 0)
        
    def test_get_user_notifications_role_filter(self):
        """测试角色过滤"""
        # 创建管理员通知
        notification = Notification.objects.create(
            title='管理员通知',
            content='这是一个管理员通知内容',
            target_roles=['admin'],
            priority='medium',
            start_date=timezone.now() - timezone.timedelta(hours=1),
            end_date=timezone.now() + timezone.timedelta(days=1),
            created_by=self.admin_user,
            is_active=True
        )
        
        # 普通用户不应看到管理员通知
        notifications = get_user_notifications(self.user)
        self.assertEqual(len(notifications), 0)
        
        # 创建所有角色通知
        notification_all = Notification.objects.create(
            title='所有角色通知',
            content='这是一个所有角色通知内容',
            target_roles=['all'],
            priority='medium',
            start_date=timezone.now() - timezone.timedelta(hours=1),
            end_date=timezone.now() + timezone.timedelta(days=1),
            created_by=self.admin_user,
            is_active=True
        )
        
        # 所有用户都应看到所有角色通知
        notifications = get_user_notifications(self.user)
        self.assertEqual(len(notifications), 1)
        self.assertEqual(notifications[0].title, '所有角色通知')
